<?php

namespace App\Http\Middleware\platform;


use App\Exceptions\ApiException;
use App\Services\AdminService;
use Closure;

class Authenticate
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $key = 'Authorization';
        if (!$request->hasHeader($key)){
            throw new ApiException(40001);
        }
        $token = $request->header($key);
        $admin = new AdminService();
        $admin->auth($token);

        $response = $next($request);
        $response->header('Access-Control-Allow-Origin', '*');
        $response->header('Access-Control-Allow-Headers', 'Origin, Content-Type, Cookie, Accept');
        $response->header('Access-Control-Allow-Methods', 'GET, POST, PATCH, PUT, OPTIONS');
        $response->header('Access-Control-Allow-Credentials', 'false');
        return $next($request);
    }


}
